package com.example.hetong_system.service;

// src/main/java/com/example/hetong_system/service/AuthService.java

import com.example.hetong_system.model.dto.LoginRequest;
import com.example.hetong_system.model.Customer;
import com.example.hetong_system.model.Employee;
import com.example.hetong_system.model.SystemAdmin;
import com.example.hetong_system.repository.CustomerRepository;
import com.example.hetong_system.repository.EmployeeRepository;
import com.example.hetong_system.repository.SystemAdminRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
@Transactional
public class AuthService {

    @Autowired
    private CustomerRepository customerRepository;

    @Autowired
    private EmployeeRepository employeeRepository;

    @Autowired
    private SystemAdminRepository systemAdminRepository;

    public ResponseEntity<?> login(LoginRequest request) {
        String username = request.getUsername();
        String password = request.getPassword();
        String role = request.getRole();

        switch (role) {
            case "customer":
                Optional<Customer> customer = customerRepository.findByContactNameAndPassword(username, password);
                if (customer.isPresent()) {
                    return ResponseEntity.ok("顾客登录成功");
                }
                break;
            case "employee":
                Optional<Employee> emp = employeeRepository.findByNameAndPassword(username, password);
                if (emp.isPresent()) {
                    return ResponseEntity.ok("员工登录成功");
                }
                break;
            case "admin":
                Optional<SystemAdmin> admin = systemAdminRepository.findByUsernameAndPassword(username, password);
                if (admin.isPresent()) {
                    return ResponseEntity.ok("管理员登录成功");
                }
                break;
            default:
                return ResponseEntity.badRequest().body("角色错误");
        }

        return ResponseEntity.status(401).body("用户名或密码错误");
    }

    // 注册员工
    public void registerEmployee(Employee employee) {
        if (employee.getName() == null || employee.getPassword() == null) {
            throw new IllegalArgumentException("姓名和密码不能为空");
        }
        employeeRepository.save(employee); // ⬅️ 调用 save 方法保存到数据库
    }

    // 注册管理员
    public void registerAdmin(SystemAdmin admin) {
        if (admin.getUsername() == null || admin.getPassword() == null) {
            throw new IllegalArgumentException("用户名和密码不能为空");
        }
        systemAdminRepository.save(admin); // ⬅️ 调用 save 方法保存到数据库
    }

    public void registerCustomer(Customer customer)
    {
        if (customer.getContactName() == null || customer.getPassword() == null
                ||
                customer.getCompanyName() == null || customer.getContactInfo() == null
        ) {
            throw new IllegalArgumentException("所有字段都不能为空"
            );
        }

        System.out.println("准备保存客户：" + customer); // 打印调试信息
//        customer.setId(null);
        customerRepository.save(customer);
        System.out.println("客户已保存"); // 确认执行到这里
    }
}
